package com.canaan.business.service.bill.impl;

import com.canaan.business.domain.bill.AdjustInBill;
import com.canaan.business.domain.bill.AdjustInBillDetail;
import com.canaan.business.mapper.bill.AdjustInBillDetailMapper;
import com.canaan.business.service.bill.IAdjustInBillDetailService;
import com.canaan.business.service.bill.IAdjustInBillService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.StringUtils;
import com.canaan.common.utils.TableNoUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 调拔入库单明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-01
 */
@Service
public class AdjustInBillDetailServiceImpl implements IAdjustInBillDetailService {
    @Resource
    private AdjustInBillDetailMapper adjustInBillDetailMapper;
    @Resource
    private IAdjustInBillService adjustInBillService;

    /**
     * 查询调拔入库单明细
     *
     * @param id 调拔入库单明细主键
     * @return 调拔入库单明细
     */
    @Override
    public AdjustInBillDetail selectAdjustInBillDetailById(Long id) {
        return adjustInBillDetailMapper.selectAdjustInBillDetailById(id);
    }

    /**
     * 查询调拔入库单明细列表
     *
     * @param adjustInBillDetail 调拔入库单明细
     * @return 调拔入库单明细
     */
    @Override
    public List<AdjustInBillDetail> selectAdjustInBillDetailList(AdjustInBillDetail adjustInBillDetail) {
        return adjustInBillDetailMapper.selectAdjustInBillDetailList(adjustInBillDetail);
    }

    /**
     * 新增调拔入库单明细
     *
     * @param adjustInBillDetail 调拔入库单明细
     * @return 结果
     */
    @Override
    public int insertAdjustInBillDetail(AdjustInBillDetail adjustInBillDetail) {
        if(StringUtils.isBlank(adjustInBillDetail.getNo())){
            adjustInBillDetail.setNo(TableNoUtil.getNo(AdjustInBillDetail.class));
        }
        if(StringUtils.isEmpty(adjustInBillDetail.getCreateBy())){
            adjustInBillDetail.setCreateBy(SecurityUtils.getUsername());
        }
        adjustInBillDetail.setCreateTime(DateUtils.getNowDate());
        return adjustInBillDetailMapper.insertAdjustInBillDetail(adjustInBillDetail);
//        refreshTotalQty(adjustInBillDetail.getBillNo());

    }

    private void refreshTotalQty(String billNo){
        AdjustInBillDetail detail = new AdjustInBillDetail();
        detail.setBillNo(billNo);
        List<AdjustInBillDetail> details = this.selectAdjustInBillDetailList(detail);
        AdjustInBill bill = adjustInBillService.selectAdjustInBillByNo(billNo);
        bill.setTotalQty(details.stream().filter(e->e.getQty()!=null).mapToLong(e -> e.getQty()).sum());
        adjustInBillService.updateAdjustInBill(bill);
    }
    /**
     * 批量新增调拔入库单明细
     *
     * @param adjustInBillDetailList 调拔入库单明细列表
     * @return 结果
     */
    @Override
    public int batchInsertAdjustInBillDetail(List<AdjustInBillDetail> adjustInBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (AdjustInBillDetail adjustInBillDetail : adjustInBillDetailList){
            if(StringUtils.isBlank(adjustInBillDetail.getNo())){
                adjustInBillDetail.setNo(TableNoUtil.getNo(AdjustInBillDetail.class));
            }
            adjustInBillDetail.setCreateBy(username);
            adjustInBillDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<adjustInBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > adjustInBillDetailList.size()) {
                endIndex = adjustInBillDetailList.size();
            }
            insertNum = insertNum + adjustInBillDetailMapper.batchInsertAdjustInBillDetail(adjustInBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改调拔入库单明细
     *
     * @param adjustInBillDetail 调拔入库单明细
     * @return 结果
     */
    @Override
    public int updateAdjustInBillDetail(AdjustInBillDetail adjustInBillDetail) {
        adjustInBillDetail.setUpdateBy(SecurityUtils.getUsername());
        adjustInBillDetail.setUpdateTime(DateUtils.getNowDate());
        int i = adjustInBillDetailMapper.updateAdjustInBillDetail(adjustInBillDetail);
        refreshTotalQty(adjustInBillDetail.getBillNo());
        return i;
    }

    /**
     * 批量修改调拔入库单明细
     *
     * @param adjustInBillDetailList 调拔入库单明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateAdjustInBillDetail(List<AdjustInBillDetail> adjustInBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (AdjustInBillDetail adjustInBillDetail : adjustInBillDetailList){
            adjustInBillDetail.setUpdateBy(username);
            adjustInBillDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<adjustInBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > adjustInBillDetailList.size()) {
                endIndex = adjustInBillDetailList.size();
            }
            updateNum = updateNum + adjustInBillDetailMapper.batchUpdateAdjustInBillDetail(adjustInBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除调拔入库单明细
     *
     * @param ids 需要删除的调拔入库单明细主键集合
     * @return 结果
     */
    @Override
    public int deleteAdjustInBillDetailByIds(Long[] ids) {
        int i = adjustInBillDetailMapper.deleteAdjustInBillDetailByIds(ids);
        final AdjustInBillDetail adjustInBillDetail = this.selectAdjustInBillDetailById(ids[0]);
        refreshTotalQty(adjustInBillDetail.getBillNo());
        return i;
    }

    /**
     * 删除调拔入库单明细信息
     *
     * @param id 调拔入库单明细主键
     * @return 结果
     */
    @Override
    public int deleteAdjustInBillDetailById(Long id) {
        return adjustInBillDetailMapper.deleteAdjustInBillDetailById(id);
    }

    /**
     * 查询调拔入库单明细
     *
     * @param no 编号
     * @return 调拔入库单明细
     */
    @Override
    public AdjustInBillDetail selectAdjustInBillDetailByNo(String no) {
        return adjustInBillDetailMapper.selectAdjustInBillDetailByNo(no);
    }

    /**
     * 批量删除调拔入库单明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteAdjustInBillDetailByNos(String[] nos) {
        return adjustInBillDetailMapper.deleteAdjustInBillDetailByNos(nos);
    }

    /**
     * 删除调拔入库单明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteAdjustInBillDetailByNo(String no) {
        return adjustInBillDetailMapper.deleteAdjustInBillDetailByNo(no);
    }
}
